package com.dz.lm.service.comp.role.impl;

import com.dz.lm.common.dto.CompUserDTO;
import com.dz.lm.common.dto.PageDto;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.utils.stereotype.ReaderRepository;
import com.dz.lm.dao.comprole.read.CompRoleDao;
import com.dz.lm.dao.compuser.write.CompUserDao;
import com.dz.lm.domain.comprole.CompRole;
import com.dz.lm.domain.comprole.CompRoleExample;
import com.dz.lm.service.comp.role.CompRoleService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class CompRoleServiceImpl implements CompRoleService {


    @Resource(name = "readCompRoleDao")
    private CompRoleDao readCompRoleDao;


    @Resource(name = "writeCompRoleDao")
    private com.dz.lm.dao.comprole.write.CompRoleDao writeCompRoleDao;

    @Resource(name = "writeCompUserDao")
    private CompUserDao writeCompUserDao;
    @Override
    public int add(CompRole role) throws ServiceException {
        return writeCompRoleDao.insertSelective(role);
    }

    @Override
    public int update(CompRole role) throws ServiceException {
        return writeCompRoleDao.updateByPrimaryKeySelective(role);
    }

    @Override
    public int delete(int roleId) throws ServiceException {
        int ret =writeCompRoleDao.deleteByPrimaryKey(roleId);
        if(ret > 0){
            writeCompUserDao.deleteByRoleId(roleId);
        }
        return ret;
    }



    @Override
    public CompRole get(int roleId) throws ServiceException {
        return readCompRoleDao.selectByPrimaryKey(roleId);
    }

    @Override
    public List<CompRole> getByComp(int compId) throws ServiceException {
       CompRoleExample ex = new CompRoleExample();
        CompRoleExample.Criteria cr = ex.createCriteria();
        cr.andCompIdEqualTo(compId);
        return readCompRoleDao.selectByExample(ex);
    }
}
